// import Vue from 'vue'
// import { Message } from 'element-ui';
import router from '../router'
import { Loading } from 'element-ui';
import axios from 'axios'
// Vue.prototype.$message = Message
// console.log("http.js username",sessionStorage.getItem('username'))
const request = axios.create({
    baseURL: window._apiUrl.webUrl,
    timeout: 10000,
    headers: {
        'Content-type': 'application/json',
    }
})
//form request
const requestByForm = axios.create({
    baseURL: window._apiUrl.webUrl,
    timeout: 10000,
    headers: {
        'Content-Type': 'multipart/form-data'
    }
})
let option = {
    fullscreen: true,
    text: "加载中",
    spinner: "el-icon-loading"
}
export function get(url, params) {
    request.defaults.headers.userId = sessionStorage.getItem("userId")
    request.defaults.headers.username = sessionStorage.getItem("username")
    let loadingInstance = Loading.service(option);
    return new Promise((resolve, reject) => {
        request.get(url, {
            params: params
        }).then(res => {
            resolve(res.data);
            loadingInstance.close();
        }).catch(err => {
            reject(err.data)
            loadingInstance.close();
        })
    });
}

export function rest(url, params) {
    request.defaults.headers.userId = sessionStorage.getItem("userId")
    request.defaults.headers.username = sessionStorage.getItem("username")
    let loadingInstance = Loading.service(option);
    return new Promise((resolve, reject) => {
        request.get(url + '/' + params).then(res => {
            resolve(res.data);
            loadingInstance.close();
        }).catch(err => {
            reject(err.data)
            loadingInstance.close();
        })
    });
}

export function post(url, params) {
    request.defaults.headers.userId = sessionStorage.getItem("userId")
    request.defaults.headers.username = sessionStorage.getItem("username")
    let loadingInstance = Loading.service(option);
    return new Promise((resolve, reject) => {
        request.post(url, JSON.stringify(params))
            .then(res => {
                resolve(res.data);
                loadingInstance.close();
            })
            .catch(err => {
                reject(err.data)
                loadingInstance.close();
            })
    });
}

export function formPost(url, formData) {
    request.defaults.headers.userId = sessionStorage.getItem("userId")
    request.defaults.headers.username = sessionStorage.getItem("username")
    let loadingInstance = Loading.service(option);
    return new Promise((resolve, reject) => {
        requestByForm.post(url, formData).then(res => {
            resolve(res)
            loadingInstance.close();
        }).catch(res => {
            reject(res.data)
            loadingInstance.close();
        })
    });
}

request.interceptors.response.use(
    response => {
        if (response.data.status == 'notLogin') {
            router.push('/login')
        }
        if (response.data.status == 'message') {  //如果是消息
            sessionStorage.removeItem('userMessage');
            sessionStorage.setItem('userMessage', JSON.stringify(response.data));
        }
        return response;
    }
)

